<?php
namespace admin;
use models\BaseDao;
use Slince\Upload\UploadHandlerBuilder;
use JasonGrimes\Paginator;
class Article extends Admin{
    public function  __construct(){
        parent::__construct();
        $this->assign('menumark','article');
        $db = new BaseDao();
        $data = $db->select('class','*',['ORDER'=>['ord'=>'ASC','id'=>'DESC']]);
        $this->assign('aclass',$data);
    }
/**
 * 文章列表页面
 */
    function index(){
        // 获取数据库操作对象
        $db = new BaseDao();

        // 分页
        $num = $_GET['num'] ?? 1;

        $prosql['ORDER'] = ['id'=>'DESC'];
        $where = [];
        if(!empty($_GET['cid']) || $_GET['cid'] != 0){
            $where['cid'] = $_GET['cid'];
            $cid = '&cid='.$_GET['cid'];
        }
        
        if(!empty($_GET['name'])){
            $where['name[~]'] = $_GET['name'];
            $name = '&name='.$_GET['name'];
        }

        $this->assign('get',$_GET);


        $totalItems = $db->count('article',$where);
       
        $itemsPerPage = PNUM;
        $currentPage = $num;
        $urlPattern = '/admin/article?num=(:num)'.$cid.$name;

        $paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

        $start = ($currentPage-1) * $itemsPerPage;
        $prosql['LIMIT'] = [$start,$itemsPerPage];
        $prosql = array_merge($prosql,$where);

        // 获取全部文章,按照ord排序
        $data = $db->select('article',['id','name','atime','clicknum'],$prosql);
        // 将数据全部分给模板
        $this->assign('data',$data);
        $this->assign('fpage',$paginator);

        //标题    
        $this->assign('title','文章列表');
        // 导入模板
        $this->display('article/index');
    }
    function add(){
        // 如果$_POST['do_submit]存在,说明是添加动作
        if(isset($_POST['do_submit'])){
            $db = new BaseDao();


            $_POST['atime'] = empty($_POST['atime']) ? time() :strtotime($_POST['atime']);


            unset($_POST['do_submit']);
            if($db->insert('article',$_POST)){
                $this->success('/admin/article','添加ok');
            }else{
                $this->error('/admin/article/add','添加fail');
            }
        }
        //标题    
        $this->assign('title','添加文章');
        $this->display('article/add');

    }

    function upload(){
            // 上传图片
            $path = TEMPDIR."/uploads/article";
            $builder = new UploadHandlerBuilder(); //create a builder.
            $handler = $builder
            //add constraints
            ->allowExtensions(['jpg','png','gif'])
            ->allowMimeTypes(['image/*'])

            ->saveTo($path) //save to local
            ->getHandler();

            $files = $handler->handle();
            $filename = $files['file']->getUploadedFile()->getClientOriginalName(); // original name

            $newfilename = date('Y-md') . '-' . uniqid() . '.' . $files['file']->getUploadedFile()->getClientOriginalExtension();

            rename($path.'/'.$filename,$path.'/'.$newfilename);
            $url = getCurUrl();
            $arr['src'] = $url.'/uploads/article/'.$newfilename;
            
            echo json_encode($arr);
    }

    function mod($id){
        $db = new BaseDao();
        $this->assign($db->get('article','*',['id'=>$id]));
        $this->assign('title',"修改文章");
        $this->display('article/mod');
    }
    function doupdate(){
        $id = $_POST['id'];
        unset($_POST['id']);
        $_POST['atime'] = empty($_POST['atime']) ? time() :strtotime($_POST['atime']);

        $db = new BaseDao();

        if($db->update('article',$_POST,['id'=>$id])){
            $this->success('/admin/article','修改ok');
        }else{
            $this->error('/admin/article/mod/'.$id,'修改fail');
        }
    }
    function del($id){
        $db = new BaseDao();
        if($db->delete('article',['id'=>$id])){
            $this->success('/admin/article','删除ok');
        }else{
            $this->error('/admin/article','删除fail');
        }
    }
    function alldel(){
        $db = new BaseDao();
        $num = 0;
        foreach($_POST['id'] as $id){
            
            $num += $db->delete('article',['id'=>$id]);
        }
        if($num > 0){
            $this->success('/admin/article',$num.'篇文章删除ok');
        }else{
            $this->success('/admin/article','删除失败');
        }
    }
}